package com.woniuxy.movie.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.movie.commons.web.util.StateData;
import com.woniuxy.movie.product.dao.ProductMovieMapper;
import com.woniuxy.movie.product.exception.ProductException;
import com.woniuxy.movie.product.form.UpdateMovieStateForm;
import com.woniuxy.movie.product.form.productmovie.AddMovieForm;
import com.woniuxy.movie.product.model.ProductMovie;
import com.woniuxy.movie.product.service.ProductMovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 电影详情表 服务实现类
 * </p>
 *
 * @author 刘存益
 * @since 2023年11月17日
 */
@Service
public class ProductMovieServiceImpl extends ServiceImpl<ProductMovieMapper, ProductMovie> implements ProductMovieService {


    @Autowired
    ProductMovieMapper productMovieMapper;

    /**
     * 新增影片信息
     *
     * @param form
     */
    @Override
    @Transactional
    public void add(AddMovieForm form) {
        String movieActors = JSONUtil.toJsonStr(form.getMovieActors());
        ProductMovie productMovie = BeanUtil.toBean(form, ProductMovie.class);
        productMovie.setMovieActors(movieActors);
        productMovie.setMovieState(StateData.MOVIE_STATE_NORMAL);
        productMovie.setMovieCreateTime(DateUtil.format(new Date(System.currentTimeMillis()), "yyyy-MM-dd HH:mm:ss"));
        productMovieMapper.insert(productMovie);
    }

    /**
     * 修改影片状态
     *
     * @param form
     */
    @Override
    public void updateMovieState(UpdateMovieStateForm form) {
        ProductMovie productMovie = productMovieMapper.selectById(form.getMovieId());
        if (productMovie == null) throw new ProductException(2010, "该影片不存在");
        if (productMovie.getMovieState().equals(StateData.MOVIE_STATE_DELETED))
            throw new ProductException(2011, "该影片已被删除");
        ProductMovie movie = new ProductMovie();
        movie.setMovieId(productMovie.getMovieId());
        if (form.getMovieState().equals(StateData.MOVIE_STATE_DISABLED)) {
            if (productMovie.getMovieState().equals(StateData.MOVIE_STATE_DISABLED))
                throw new ProductException(2012, "请勿重复下架");
            //查询是否有排场
            movie.setMovieState(StateData.MOVIE_STATE_DISABLED);
        } else if (form.getMovieState().equals(StateData.MOVIE_STATE_NORMAL)) {
            if (productMovie.getMovieState().equals(StateData.MOVIE_STATE_NORMAL))
                throw new ProductException(2013, "请勿重复上架");
            movie.setMovieState(StateData.MOVIE_STATE_NORMAL);
        } else if (form.getMovieState().equals(StateData.MOVIE_STATE_DELETED)) {
            //查询是否有排场
            movie.setMovieState(StateData.MOVIE_STATE_DELETED);
        } else {
            throw new ProductException(2014, "不合法");
        }
        movie.setMovieUpdateTime(DateUtil.format(new Date(System.currentTimeMillis()), "yyyy-MM-dd HH:mm:ss"));
        movie.setMovieUpdateUser(form.getMovieUpdateUser());
        productMovieMapper.updateById(movie);
    }
}
